package snmyj.agile.tdd.tddbook;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Template {
	private Map<String, String> variables;
	private String templateText;

	public Template(String templateText) {
		this.variables = new HashMap<String, String>();
		this.templateText = templateText;
	}

	public void set(String variable, String value) {
		this.variables.put(variable, value);
	}

	public String evaluate() {
		String result = templateText;
		result = replaceVariables(result);
		checkForMissingValues(result);
		return result;
	}

	private String replaceVariables(String result) {
		for (Entry<String, String> entry : this.variables.entrySet()) {
			String regex = "\\$\\{" + entry.getKey() + "\\}";
			result = result.replaceAll(regex, entry.getValue());
		}
		return result;
	}

	private void checkForMissingValues(final String result) {
		Matcher matcher = Pattern.compile(".*\\$\\{.+\\}.*").matcher(result);
		if (matcher.find()) {
			throw new MissingValueException("No value for " + matcher.group() + "");
		}
	}
}
